package com.seal.yoyoshop.web;

import com.seal.yoyoshop.bin.*;
import com.seal.yoyoshop.service.GoodsService;
import com.seal.yoyoshop.service.ItemService;
import com.seal.yoyoshop.service.OrderService;
import com.seal.yoyoshop.service.TypeService;
import com.seal.yoyoshop.service.impl.GoodsServiceImpl;
import com.seal.yoyoshop.service.impl.ItemServiceImpl;
import com.seal.yoyoshop.service.impl.OrderServiceImpl;
import com.seal.yoyoshop.service.impl.TypeServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "PlaceOrderServlet", value = "/index/placeOrder")
public class PlaceOrderServlet extends HttpServlet {

    private TypeService typeService = new TypeServiceImpl();
    private OrderService orderService = new OrderServiceImpl();
    private GoodsService goodsService = new GoodsServiceImpl();
    private ItemService itemService = new ItemServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        if(user == null){
            response.sendRedirect("login");
        }else {
            Order order = (Order) session.getAttribute("order");
            List<Item> itemList = order.getItemList();
            for(int i = 0; i < itemList.size();i++){
                Item item = itemList.get(i);
                int goodsId = item.getGoodId();
                int amount = item.getAmount();
                Goods goods = goodsService.getGoodById(goodsId);
                if(goods.getStock() < amount ){
                    request.setAttribute("msg","下单失败，商品"+ goods.getName() +"库存不足");
                    request.getRequestDispatcher("/index/cart").forward(request, response);
                    return;
                }

            }

            boolean isSuccess = orderService.addOrder(order);
            if(isSuccess){
//                获取订单id
                int id = orderService.getIdByOrder(order);
                order.setId(id);
//                根据订单信息添加购买项
                itemService.addItemsByOrder(order);
//                下单完清空购物车
                session.removeAttribute("order");
                request.setAttribute("order",order);
                //            破导航栏，建议封装一下
                List<Type> typeList = typeService.getTypeList();
                request.setAttribute("typeList", typeList);
                //            转发
                request.getRequestDispatcher("/index/pay.jsp").forward(request, response);
            }else {
                request.setAttribute("msg","下单失败，创建订单出错");
                request.getRequestDispatcher("/index/cart").forward(request,response);
            }


        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
